package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.AskAnswerDao;
import com.jiantong.lawEnforcing.impl.manager.DatasManager;
import com.jiantong.lawEnforcing.model.AskAnswer;
import com.jiantong.lawEnforcing.service.AskAnswerService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 问答
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class AskAnswerServiceImpl implements AskAnswerService {


    private final AskAnswerDao askAnswerDao;
    private final DatasManager datasManager;
    @Autowired
    public AskAnswerServiceImpl(AskAnswerDao askAnswerDao, DatasManager datasManager) {
        this.askAnswerDao = askAnswerDao;
        this.datasManager = datasManager;
    }

    /**
     * id 查找
     *
     * @param id
     * @return AskAnswer
     */
    @Override
    public Response<AskAnswer> finById(Long id) {
        try {
            AskAnswer askAnswer = askAnswerDao.findById(id);
            if (askAnswer != null) {
                return Response.ok(askAnswer);
            } else {
                log.error("askAnswer (id={}) is not found", id);
                return Response.fail("askAnswer.not.found");
            }
        }catch (Exception e){
            log.error("failed to find askAnswer(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("askAnswer.find.fail");
        }
    }

    /**
     * ids 查找
     *
     * @param ids
     * @return News
     */
    @Override
    public Response<List<AskAnswer>> finByIds(List<Long> ids) {
        try {
            return Response.ok(askAnswerDao.findByIds(ids));
        } catch (Exception e) {
            log.error("根据idList查询失败,原因:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("根据idList查询失败");
        }
    }

    /**
     * 创建新闻新单据
     *
     * @param askAnswer 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(AskAnswer askAnswer) {
        try {
            askAnswerDao.create(askAnswer);
            return Response.ok(askAnswer.getId());
        }catch (Exception e) {
            log.error("failed.to.create.askAnswer:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.askAnswer.failed");
        }
    }

    /**
     * 创建问答列表,并且写入到idList字段中 更新datas
     *
     * @param askAnswer
     * @param lawCase
     * @param writType
     * @param idList
     * @return
     */
    @Override
    public Response<Long> createAndList(AskAnswer askAnswer, Long lawCase, Integer writType, String idList) {
        try {
            Long askAnswerId =datasManager.createAndList(askAnswer, lawCase, writType, idList);
            return Response.ok(askAnswerId);
        } catch (Exception e) {
            log.error("failed.to.create.askAnswer:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.askAnswer.failed");
        }
    }

    /**
     * 更新新闻的单据
     *
     * @param askAnswer 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(AskAnswer askAnswer) {
        try{
            return Response.ok(askAnswerDao.update(askAnswer));
        }catch (Exception e){
            log.error("failed.to.update.askAnswer:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.askAnswer.failed");
        }
    }
}
